package com.zzy.blog.handle;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class HeartBeatDispatchThread extends Thread{

	public static HeartBeatDispatchThread hbdThread = null;
	private BlockingQueue<HeartBeat> heartBeats = new LinkedBlockingQueue<HeartBeat>();
	private ExecutorService service = Executors.newFixedThreadPool(5);
	
	private HeartBeatDispatchThread() {
		this.setName("HeartBeatDispatchThread");
	}
	
	public synchronized static HeartBeatDispatchThread getInstance() {
		if(hbdThread == null) {
			hbdThread = new HeartBeatDispatchThread();
		}
		return hbdThread;
	}
	
	public synchronized void addHeartBeat(HeartBeat heartBeat) throws InterruptedException {
		heartBeats.put(heartBeat);
	}
	
	@Override
	public void run(){
		while(true) {
			try {
				HeartBeat heartBeat = heartBeats.take();
				HeartBeatThread hbThread = new HeartBeatThread(heartBeat);
				service.execute(hbThread);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
}
